package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.AddAddressReq;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_post;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/1/18.
 * <p>
 * 添加新地址
 */

public class AddNewAddress extends BaseActivity implements View.OnClickListener {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private EditText et_receiver, et_phone_number, et_postcode, et_address;
    private ImageView img_back;
    private LinearLayout lly_location;
    private int province_code, city_code, area_code, location_code;
    private String province_name, city_name, area_name, location_name;
    private TextView tv_location, tv_street;
    private String secret;
    private TextView tv_add_address;
    private LinearLayout lly_street;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_new_address);

        mActivity = this;
        init();
//        initWindow();
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A000000), 0);

    }

    private void init() {
        Log.e("ssss", "返回走");
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();

        lly_street = (LinearLayout) mActivity.findViewById(R.id.lly_street);
        tv_location = (TextView) mActivity.findViewById(R.id.tv_location);
        tv_street = (TextView) mActivity.findViewById(R.id.tv_street);


        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        et_receiver = (EditText) mActivity.findViewById(R.id.et_receiver);
        et_phone_number = (EditText) mActivity.findViewById(R.id.et_phone_number);
        et_postcode = (EditText) mActivity.findViewById(R.id.et_postcode);
        et_address = (EditText) mActivity.findViewById(R.id.et_address);
        lly_location = (LinearLayout) mActivity.findViewById(R.id.lly_location);
        tv_add_address = (TextView) mActivity.findViewById(R.id.tv_add_address);
        if (myApplication.getName1() != null) {
            et_receiver.setText(myApplication.getName1());
        }
        if (myApplication.getMobile1() != null) {
            et_phone_number.setText(myApplication.getMobile1());
        }
        if (myApplication.getZip_code1() != null) {
            et_postcode.setText(myApplication.getZip_code1());
        }
        if (myApplication.getAddress1() != null) {
            et_address.setText(myApplication.getAddress1());
        }
        if (myApplication.getProvince1() != null || myApplication.getCity1() != null || myApplication.getArea1() != null) {
            Log.e("ssss", "地址11111==》" + myApplication.getProvince1() + myApplication.getCity1() + myApplication.getArea1());

            tv_location.setText(myApplication.getProvince1() + myApplication.getCity1() + myApplication.getArea1());
        }
        if (myApplication.getLocation1() != null) {
            tv_street.setText(myApplication.getLocation1());
        }

        img_back.setOnClickListener(this);
        lly_location.setOnClickListener(this);
        tv_add_address.setOnClickListener(this);
        lly_street.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back://返回
                finish();
                break;
            case R.id.lly_location://所在地区
                myApplication.setName1(et_receiver.getText().toString());
                myApplication.setMobile1(et_phone_number.getText().toString());
                myApplication.setZip_code1(et_postcode.getText().toString());
                myApplication.setAddress1(et_address.getText().toString());
                myApplication.setIntent_code(1);
                Intent intent = new Intent(AddNewAddress.this, ChooseAddressProvince.class);
                startActivity(intent);
                break;
            case R.id.lly_street://街道
                if (tv_location.getText().toString() == "") {
                    CommonUtil.showToast(mActivity, "请先选择所在地区");
                } else {
                    myApplication.setIntent_code(1);
                    startActivity(new Intent(AddNewAddress.this, ChooseAddressLocation.class));
                }
                break;

            case R.id.tv_add_address://添加为新地址
                if (et_receiver.getText().toString().equals("") || et_phone_number.getText().toString().equals("") || et_postcode.getText().toString().equals("")
                        || tv_location.getText().toString().equals("") || tv_street.getText().toString().equals("") || et_address.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请将信息填写完整");
                    return;
                } else {
                    try {
                        AddAddress();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;

        }
    }

    //添加地址
    private void AddAddress() {
        CustomProgressDialog.createDialog(mActivity).show();
        AddAddressReq req = new AddAddressReq();
        Log.e("ssss", "省份ID1111==>" + myApplication.getProvince_id1());
        req.setProvince(myApplication.getProvince_id1());
        req.setCity(myApplication.getCity_id1());
        req.setArea(myApplication.getArea_id1());
        req.setLocation(myApplication.getLocation_id1());
        req.setAddress(et_address.getText().toString());
        req.setName(et_receiver.getText().toString());
        req.setZip_code(et_postcode.getText().toString());
        req.setMobile(et_phone_number.getText().toString());
        String url = Const.COMMON_ADDRESS + req.urlString();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        try {
            String source = req.getString();
            Log.e("ssss", "source==>" + source);

            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        params.put("secret", secret);
        Log.e("ssss", "secret==>" + secret);

        Async_http_post.post(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "add_address_json==" + response.toString());
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, Activity.RESULT_OK);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");

            }
        });
    }
}
